<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车牌号" prop="licensePlate">
      <el-input v-model="dataForm.licensePlate" placeholder="车牌号"></el-input>
    </el-form-item>
    <el-form-item label="车辆颜色" prop="color">
      <el-input v-model="dataForm.color" placeholder="车辆颜色"></el-input>
    </el-form-item>
    <el-form-item label="车辆描述信息" prop="description">
      <el-input v-model="dataForm.description" placeholder="车辆描述信息"></el-input>
    </el-form-item>
    <el-form-item label="车辆图片【字符串数组使用,分割】" prop="images">
      <el-input v-model="dataForm.images" placeholder="车辆图片【字符串数组使用,分割】"></el-input>
    </el-form-item>
    <el-form-item label="车辆的车主ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="车辆的车主ID"></el-input>
    </el-form-item>
    <el-form-item label="删除记录" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除记录"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          licensePlate: '',
          color: '',
          description: '',
          images: '',
          userId: '',
          isDeleted: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          licensePlate: [
            { required: true, message: '车牌号不能为空', trigger: 'blur' }
          ],
          color: [
            { required: true, message: '车辆颜色不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '车辆描述信息不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '车辆图片【字符串数组使用,分割】不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '车辆的车主ID不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除记录不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/car/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.licensePlate = data.car.licensePlate
                this.dataForm.color = data.car.color
                this.dataForm.description = data.car.description
                this.dataForm.images = data.car.images
                this.dataForm.userId = data.car.userId
                this.dataForm.isDeleted = data.car.isDeleted
                this.dataForm.createTime = data.car.createTime
                this.dataForm.updateTime = data.car.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/car/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'licensePlate': this.dataForm.licensePlate,
                'color': this.dataForm.color,
                'description': this.dataForm.description,
                'images': this.dataForm.images,
                'userId': this.dataForm.userId,
                'isDeleted': this.dataForm.isDeleted,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
